// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import BaseView from "../base/BaseView";
import RankTrendComponent from "../base/components/RankTrendComponent";
import TimeModule from "../module/TimeModule";
import TushareModule from "../module/TushareModule";
import DropList from "./items/DropList";

const {ccclass, property} = cc._decorator;


const TradeDayConfig = {
    data: ['1', '2', '3', '5', '8', '10', '15', '20', '30', '50', '80', '100', '120', '150','200'],
    value: [1, 2, 3, 5, 8, 10, 15, 20, 30, 50, 80, 100, 120, 150, 200],
    defaultIndex: 12,
};

@ccclass
export default class EmoteTrendView extends BaseView {

    @property(cc.Label)
    labelText: cc.Label = null;

    @property(cc.Prefab)
    rankTrendPrefab: cc.Prefab = null;

    @property(cc.Node)
    rankLayer: cc.Node = null;

    @property(DropList)
    dropTrade: DropList = null;

    @property(DropList)
    dropType: DropList = null;
    
    rankTrend: RankTrendComponent = null;

    @property(cc.Label)
    startDate: cc.Label = null;
    
    @property(cc.Label)
    endDate: cc.Label = null;

    limitList: any;

    onLoad () {
        let rankNode = cc.instantiate(this.rankTrendPrefab);
        this.rankLayer.addChild(rankNode);
        this.rankTrend = rankNode.getComponent(RankTrendComponent);

        this.dropTrade.setData(TradeDayConfig.data, TradeDayConfig.defaultIndex);

        this.dropType.setData(['腾落指数', '权重', '连板高度', '涨停数量', '跌停数量', '炸板数量'], 0);
        this.dropType.setSelectCallback(()=>{
            this.updateView();
        });
    }

    onClickKLineNode (info) {
        this.createDialog(BaseView.Views.KLineNodeView, info, true);
    }

    onBtnClose () {
        this.node.active = false;
    }

    updateView () {
        let list = this.limitList;
        let type = this.dropType.getSelectIndex();
        let maxRank = 0;
        let maxLow = 10000;
        for (let index = 0; index < list.length; index++) {
            const item = list[index];
            if (type == 0) {
                item.rank = item.upLimitWeight - item.downLimitCount;
                if (item.rank < 0) {
                    item.rank = 0;
                }
                if (item.rank > 200) {
                    item.rank = 200;
                }
            } else if (type == 1) {
                item.rank = item.limitTimes;
            } else if (type == 2) {
                item.rank = item.upLimitCount;
            } else if (type == 3) {
                item.rank = item.downLimitCount;
            } else if (type == 4) {
                item.rank = item.zhaCount;
            }

            if (item.rank > maxRank) {
                maxRank = item.rank;
            }
            if (item.rank < maxLow) {
                maxLow = item.rank;
            }
        }
        
        let nodeWidth = Math.floor((this.node.width - 150) / (list.length + 1));
        if (nodeWidth < 10) {
            nodeWidth = 10;
        } 

        if (nodeWidth > 40) {
            nodeWidth = 40;
        }

        this.startDate.string = list[0].date.slice(4,8);
        this.endDate.string = list[list.length - 1].date.slice(4,8);
        this.startDate.node.x = nodeWidth;
        this.endDate.node.x = nodeWidth * list.length;

        let rankData = {
            maxRank: maxRank,
            list: list,
            maxLow: maxLow,
        };
        this.rankTrend.setEmoteData(rankData, nodeWidth);
    }

    async onBtnSearch () {
        let index = this.dropTrade.getSelectIndex();
        let tradeCount = TradeDayConfig.value[index];
        let dates = TimeModule.getInstance().getRecentTradeDates(tradeCount);
        let list = await TushareModule.getInstance().getDailyLimitList(dates);

        this.limitList = list.reverse();
        this.updateView();
    }

    // update (dt) {}
}
